package com.example.administrator.caigou51.activity;

import android.content.Intent;
import android.os.Bundle;

import com.example.administrator.caigou51.EventBusClass.GotoFragment;
import com.example.administrator.caigou51.EventBusClass.UpdateGoodsListActivity;
import com.example.administrator.caigou51.EventBusClass.UpdateZhuanQuByCatID;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.bean.CheckOrderBean;
import com.example.administrator.caigou51.recyclerCard.basic.MaterialListView;
import com.example.administrator.caigou51.recyclerCard.card.GoodsListCard;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2016/8/31/031.
 */
public class GoodsListActivity extends GBaseActivity {
    MaterialListView material_listview;
    public static final String TagList="TagList";
    List<CheckOrderBean.GoodsEntity> goodsEntityList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentViewFor720_1280(R.layout.template_materiallistview_layout);
        EventBus.getDefault().register(this);
        findViewByIds();
        initDatas();
        fillArrayInListView();
    }

    private void findViewByIds() {
        material_listview= (MaterialListView) findViewById(R.id.material_listview);
    }
    private void initDatas() {
        setTopTitle("商品清单");
        goodsEntityList= (List<CheckOrderBean.GoodsEntity>) getIntent().getSerializableExtra(TagList);
    }
    private void fillArrayInListView() {
        for (int i=0;i<goodsEntityList.size();i++)
        {
            GoodsListCard goodsListCard=new GoodsListCard(this);
            goodsListCard.setGoodsEntityList(goodsEntityList);
            goodsListCard.setGoodsEntity(goodsEntityList.get(i));
            if (i==goodsEntityList.size()-1)
            {
                goodsListCard.setEnd(true);
            }
            material_listview.add(goodsListCard);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(UpdateGoodsListActivity updateGoodsListActivity) {
        material_listview.clear();
        if (updateGoodsListActivity.getGoodsEntityList().size()==0)
        {
            Intent intent=new Intent(this,MainActivity.class);
            startActivity(intent);
            EventBus.getDefault().post(new GotoFragment(MainActivity.TAB_ShouYe));
        }else {
            for (int i = 0; i < updateGoodsListActivity.getGoodsEntityList().size(); i++) {
                GoodsListCard goodsListCard = new GoodsListCard(this);
                goodsListCard.setGoodsEntityList(updateGoodsListActivity.getGoodsEntityList());
                goodsListCard.setGoodsEntity(updateGoodsListActivity.getGoodsEntityList().get(i));
                if (i == updateGoodsListActivity.getGoodsEntityList().size() - 1) {
                    goodsListCard.setEnd(true);
                }
                material_listview.add(goodsListCard);
            }
        }

    }


}
